﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Activities;
using Blackbaud.AppFx.XmlTypes;
using Blackbaud.AppFx.XmlTypes.DataForms;
using Blackbaud.AppFx.WebAPI.ServiceProxy;
using BBECServiceBusAdapter_CodeGenLib.AddForms.ServiceBusAdapterLog;

namespace BBECServiceBusAdapter.Activities
{
    public sealed class AddServiceBusAdapterLogEntry : NativeActivity
    {
        #region Activity Arguments
        public InArgument<Helper.ErrorLevel> EntryTypeCode { get; set; }
        public InArgument<string> Details { get; set; }
        #endregion

        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                // Obtain the runtime value of the Text input argument
                //string text = context.GetValue(this.Data.).ToString;
                String inDetails = context.GetValue(this.Details);
                Helper.ErrorLevel inEntryTypeCode = context.GetValue(this.EntryTypeCode);

                ServiceBusAdapterLogAddDataFormData  frmData = new ServiceBusAdapterLogAddDataFormData();
                //frmData.ConstituentID = new Guid(inData.ConstituentID);
                frmData.DETAILS = inDetails;
                
                if (inEntryTypeCode == Helper.ErrorLevel.Error) {
                    frmData.ENTRYTYPECODE = ServiceBusAdapterLogAddDataFormData.ENTRYTYPECODEVALUES.Error;
                }
                if (inEntryTypeCode == Helper.ErrorLevel.Information)
                {
                    frmData.ENTRYTYPECODE = ServiceBusAdapterLogAddDataFormData.ENTRYTYPECODEVALUES.Information;
                }
                if (inEntryTypeCode == Helper.ErrorLevel.Warning)
                {
                    frmData.ENTRYTYPECODE = ServiceBusAdapterLogAddDataFormData.ENTRYTYPECODEVALUES.Warning;
                }
                
                frmData.CORRELATIONID = context.WorkflowInstanceId;
                frmData.SERVERNAME = Environment.MachineName;


                frmData.Save(new Blackbaud.AppFx.WebAPI.AppFxWebServiceProvider(Blackbaud.AppFx.Workflow.CatalogActivities.WebApiUtility.GetAppFxWebApiProvider(context).ConnectionString));

                //Helper.CreateServiceBusAdapterLogEntry(
                //    EntryTypeCode.Get<Helper.ErrorLevel>(context), 
                //    context.WorkflowInstanceId, 
                //    Details.Get<string>(context));
     
            
            }
            catch (Exception e)
            {
                // (gulp)
            }
        }
    }
}